package com.fos.BluetoothStory.util;

import java.io.ByteArrayOutputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.annotation.SuppressLint;

import com.fos.BluetoothStory.data.TFMusicState;

/**
 * 命令解析==>数据工具类
 * 
 * @author Administrator
 * 
 */
public class BTRUtil {

	// 得到命令编码
	public static String getCmd(String cmd) {
		return cmd.substring(6, 8);
	}

	// 检查包确认标识符
	public static boolean cCheckSign(String res) {
		String flag = res.substring(10, 12);
		return "55".equals(flag);
	}

	// 解析出产品数据:产品名称，软件版本，歌曲名
	public static String cGetProductInfo(String res) {
		String result = res.substring(12, res.length() - 6);
		return Hex2String(result);
	}

	// 小端对齐
	public static String cAlign(String res) {
		String result = "";
		for (int i = 0; i < res.length() / 4; i++) {
			String one = res.substring(i * 4, i * 4 + 4);
			String head = one.substring(0, 0 + 2);
			String foot = one.substring(2, 4);
			result += foot + head;
		}
		return result;
	}

	// 解析出蓝牙信号 4c,1107004c00010001550012
	public static int cGetSignal(String res) {
		int result = Integer.parseInt(res.substring(14, 16), 16);
		return result;
	}

	// 解析出蓝牙电压 37,1107003702552010c50012
	public static int cGetVolgate37(String res) {
		int result1 = Integer.parseInt(res.substring(12, 14), 16);
		int result2 = Integer.parseInt(res.substring(14, 16), 16);
		return result1+result2*256;
	}
	
	public static int cGetVolgate4d(String res) {
		int result1 = Integer.parseInt(res.substring(14, 16), 16);
		int result2 = Integer.parseInt(res.substring(16, 18), 16);
		return result1+result2*256;
	}

	//fan 解析出歌曲目录编号
	public static String cGetMusicPNo(String res) {
		String r1 = res.substring(14, 16);
		String r2 = res.substring(16, 18);
		return r2+r1;
	}
	
	//fan 解析出歌曲编号
	public static String cGetMusicNo(String res) {
		String r1 = res.substring(18, 20);
		String r2 = res.substring(20, 22);
		return r2+r1;
	}

	// unicode转中文
	public static String unicodeToString(String str) {

		Pattern pattern = Pattern.compile("(\\\\u(\\p{XDigit}{4}))");
		Matcher matcher = pattern.matcher(str);
		char ch;
		while (matcher.find()) {
			ch = (char) Integer.parseInt(matcher.group(2), 16);
			str = str.replace(matcher.group(1), ch + "");
		}
		return str;
	}

	// 获取命令信息
	public static String cGetCommandInfo(String res) {
		return res.substring(8, 10);
	}

	// 解析出歌曲名字
	public static String cGetMusicName(String res) {
		String result = "";
		String info = cAlign(res.substring(22, res.length() - 6));
		// String info =cAlign(res);
		System.out.println(info);
		for (int i = 0; i < info.length() / 4; i++) {
			result += "\\u" + info.substring(i * 4, i * 4 + 4);
		}
		return result;
	}

	// 将16进制转换为字符串
	@SuppressLint("DefaultLocale") 
	public static String Hex2String(String bytes) {
		bytes = bytes.toUpperCase();
		String hexString = "0123456789ABCDEF";
		ByteArrayOutputStream baos = new ByteArrayOutputStream(
				bytes.length() / 2);
		// 将每2位16进制整数组装成一个字节
		for (int i = 0; i < bytes.length(); i += 2)
			baos.write((hexString.indexOf(bytes.charAt(i)) << 4 | hexString
					.indexOf(bytes.charAt(i + 1))));
		return new String(baos.toByteArray());
	}

	/**
	 * 36,11060036025503960012 获取当前的播放模式0, 1 ,2 3
	 */
	public static void cGetMode(String res) {
		// T卡播放
		if(AppConstance.sPlayState == 3){
		TFMusicState.getInstance().setTFPlayMode(
				Integer.parseInt(res.substring(13, 14)));
		}
		
		// U盘播放
		else if(AppConstance.sPlayState == 1){
			TFMusicState.getInstance().setUSBPlayMode(
					Integer.parseInt(res.substring(13, 14)));				
		}
	}

	/**
	 * 30,110600300255018e0012 获取当前的播放状态
	 */
	public static void cGetPlayState(String res) {
		int i = Integer.parseInt(res.substring(13, 14));
		if (i == 0) {
			TFMusicState.getInstance().setIsPauseState(true);
		} else {
			TFMusicState.getInstance().setIsPauseState(true);
		}
	}

	

}
